package com.sl.au.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Protype;
import com.sl.au.service.AccountsReceivableTotalService;
import com.sl.utils.ExdeUtil;

@RestController
@RequestMapping(value="/ui/accountsreceivabletotal")
public class AccountsReceivableTotalController extends BaseController{
	
	@Autowired
	AccountsReceivableTotalService accountsReceivableTotalService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("accountsreceivable/accountsreceivableinfo");
		return modelAndView;
	}
	
	@RequestMapping(value = "getTrendChart", method = RequestMethod.GET)
	public Map<String, Object> getTrendChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		//获取应收账（时间范围内）
		List listTotalSum= accountsReceivableTotalService.getPayPlanChartsService(0,request.getParameter("beginDate"), request.getParameter("endDate"), null);
		//获取时间范围内
		List CTlistTotalSum= accountsReceivableTotalService.getPayPlanChartsService(1,request.getParameter("beginDate"), request.getParameter("endDate"), null);
		List<Float> seriesSumTotal = new ArrayList<Float>();//获取系统所有客户（时间范围内）
		List<Float> CTseriesSumTotal = new ArrayList<Float>();//获取时间范围内成功的客户
		List<String> Axis = new ArrayList<String>();//横坐标
		//获取系统所有客户（时间范围内）
		try {
			Date d1 = new SimpleDateFormat("yyyy-MM").parse(request.getParameter("beginDate"));
			Date d2 = new SimpleDateFormat("yyyy-MM").parse(request.getParameter("endDate"));//定义结束日期
			Calendar dd = Calendar.getInstance();//定义日期实例
			Calendar dd2 = Calendar.getInstance();//定义日期实例
			dd.setTime(d1);
			dd2.setTime(d2);
			dd2.add(Calendar.MONTH, 1);//进行当前日期月份加1
			while(dd.getTime().before(dd2.getTime())){//判断是否到结束日期
				boolean isFind = false;
				for(Object row : listTotalSum){
					Object[] cells = (Object[]) row;
					if(cells[0].toString().equals(ExdeUtil.datechange(dd.getTime(), "yyyy-MM"))){
						seriesSumTotal.add(Float.parseFloat(cells[1].toString()));
						isFind = true;
						break;
					}
				}
				if(!isFind){
					seriesSumTotal.add(new Float(0));
				}
				dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		////获取时间范围内成功的客户
		try {
			Date d1 = new SimpleDateFormat("yyyy-MM").parse(request.getParameter("beginDate"));
			Date d2 = new SimpleDateFormat("yyyy-MM").parse(request.getParameter("endDate"));//定义结束日期
			Calendar dd = Calendar.getInstance();//定义日期实例
			Calendar dd2 = Calendar.getInstance();//定义日期实例
			dd.setTime(d1);
			dd2.setTime(d2);
			dd2.add(Calendar.MONTH, 1);//进行当前日期月份加1
			while(dd.getTime().before(dd2.getTime())){//判断是否到结束日期
				boolean isFind = false;
				for(Object row : CTlistTotalSum){
					Object[] cells = (Object[]) row;
					if(cells[0].toString().equals(ExdeUtil.datechange(dd.getTime(), "yyyy-MM"))){
						CTseriesSumTotal.add(Float.parseFloat(cells[1].toString()));
						isFind = true;
						break;
					}
				}
				if(!isFind){
					CTseriesSumTotal.add(new Float(0));
				}
				Axis.add(dd.get(Calendar.YEAR) +"年" + (dd.get(Calendar.MONTH)+1) + "月");
				dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		map.put("Axis", Axis);
		map.put("seriesSumTotal", seriesSumTotal);
		map.put("CTseriesSumTotal", CTseriesSumTotal);
		return map;
	}
	
	@RequestMapping(value = "getChanceDeptChart", method = RequestMethod.GET)
	public Map<String, Object> getChanceDeptChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		//部门分析所有
		List list = accountsReceivableTotalService.getPayPlanChartsService(2, request.getParameter("beginDate"), request.getParameter("endDate"), null);
		//部门分析成功
		List successlist = accountsReceivableTotalService.getPayPlanChartsService(3, request.getParameter("beginDate"), request.getParameter("endDate"), null);
		List<Float> series = new ArrayList<Float>();//所有
		List<Float> series2 = new ArrayList<Float>();//成功
		String x[] = {"销售处","成套处","外贸处"};
		List<String> Axis = new ArrayList<String>();
		for(int i=0;i<x.length;i++){
			Axis.add(x[i]);
			boolean yOn1 = false;
			boolean yOn2 = false;
			for (Object row : list) {  
		        Object[] cells = (Object[]) row;
		        if(cells[0].toString().equals(x[i])){
		        	yOn1 = true;
		        	series.add(Float.parseFloat(cells[1].toString()));
		        	break;
		        }
		    }
			if(!yOn1){
				series.add(0f);
	        }
			for (Object row : successlist) {  
		        Object[] cells = (Object[]) row;
		        if(cells[0].toString().equals(x[i])){
		        	yOn2 = true;
		        	series2.add(Float.parseFloat(cells[1].toString()));
		        	break;
		        }
		    }
			if(!yOn2){
				series2.add(0f);
	        }
		}
		map.put("Axis", Axis);
		map.put("series", series);
		map.put("series2", series2);
		return map;
	}
	
	/***
	 * 获取片区饼图
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getAreaChart", method = RequestMethod.GET)
	public Map<String, Object> getAreaChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String a = request.getParameter("beginDate");
		String b = request.getParameter("endDate");
		String c =  request.getParameter("deptName");
		String d =  (request.getParameter("isSuccess")).toString();
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		List list;
		if(d.equals("应收账金额")){
			list = accountsReceivableTotalService.getPayPlanChartsService(4, request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("deptName"));
		}else{
			list = accountsReceivableTotalService.getPayPlanChartsService(5, request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("deptName"));
		}
		//List list = accountsReceivableTotalService.getPayPlanChartsService(2, request.getParameter("beginDate"), request.getParameter("endDate"), request.getParameter("deptName"));
		List<Protype> returnList = new ArrayList<Protype>();
		for (Object row : list) {  
	        Object[] cells = (Object[]) row;
	        Protype protype = new Protype();
	        protype.setValue(Float.parseFloat(cells[1].toString()));
	        protype.setName(cells[0].toString());
	        returnList.add(protype);
	    }
		map.put("data", returnList);
		return map;
	}
	
	/***
	 * 获取应收账饼图
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "yonChart", method = RequestMethod.GET)
	public Map<String, Object> yonChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String a = request.getParameter("beginDate");
		String b = request.getParameter("endDate");
		String c =  request.getParameter("deptName");
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		List list = accountsReceivableTotalService.getPayPlanChartsService(6, request.getParameter("beginDate"), request.getParameter("endDate"), null);
		List<Protype> returnList = new ArrayList<Protype>();
		for (Object row : list) {
	        Object[] cells = (Object[]) row;
	        Protype protype = new Protype();
	        protype.setValue(Float.parseFloat(cells[1].toString()));
	        protype.setName(cells[0].toString());
	        returnList.add(protype);
	    }
		map.put("data", returnList);
		return map;
	}
	
	/***
	 * 获取回款类型饼图
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "countChart", method = RequestMethod.GET)
	public Map<String, Object> countChart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String a = request.getParameter("beginDate");
		String b = request.getParameter("endDate");
		String c =  request.getParameter("deptName");
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		List list = accountsReceivableTotalService.getPayPlanChartsService(7, request.getParameter("beginDate"), request.getParameter("endDate"), null);
		List<Protype> returnList = new ArrayList<Protype>();
		for (Object row : list) {
	        Object[] cells = (Object[]) row;
	        Protype protype = new Protype();
	        protype.setValue(Float.parseFloat(cells[1].toString()));
	        protype.setName(cells[0].toString());
	        returnList.add(protype);
	    }
		map.put("data", returnList);
		return map;
	}
	

}
